using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Threading;
using HslCommunication;
using HslCommunication.Core;
using HslCommunication.Profinet.Melsec;
using HslCommunication.Profinet.Siemens;
using HslCommunicationEx.TypesEx;
using LambdaBuilder.Resolver;
using Microsoft.CSharp.RuntimeBinder;
using Catastrophe.AutoSulution.EDC;
using Catastrophe.Common;
using Catastrophe.Common.AttributeEx;

namespace HslCommunicationEx.HslNet
{
	public class NetWorkDeviceOpr : BaseNetDeviceOpr
	{
		private readonly IConnectState _iConnectState;

		private readonly IReadWriteNet _readWriteNet;

		public NetWorkDeviceOpr(IReadWriteNet readWriteNet)
		{
			this._iConnectState = readWriteNet as IConnectState;
			if (this._iConnectState == null)
			{
				throw new Exception("未实现接口IConnectStatereadWriteNet");
			}
			this._readWriteNet = readWriteNet ?? throw new ArgumentNullException("readWriteNet");
		}

		public override OperateResult ReadSingle<T>(string filedName, ushort len = 0)
		{
			Tuple<OperateResult<byte[]>, PropertyInfo> tuple = this.ReadSingleBase<T>(filedName, len);
			if (tuple.Item1?.IsSuccess ?? false)
			{
				object value = EnDeCode.ConvertByteBufToT(tuple.Item2.PropertyType, tuple.Item1.Content, 0, base.EndianMode);
				return new OperateResultEx<byte[]>
				{
					IsSuccess = true,
					Message = "",
					Value = value,
					Content = tuple.Item1.Content
				};
			}
			return new OperateResultEx<byte[]>
			{
				IsSuccess = false,
				Message = ((tuple.Item1 != null) ? tuple.Item1.Message : "未知错误")
			};
		}

		private Tuple<OperateResult<byte[]>, PropertyInfo> ReadSingleBase<T>(string filedName, ushort len = 0)
		{
			if (filedName == null)
			{
				throw new ArgumentNullException("filedName");
			}
			PropertyInfo propertyInfo = typeof(T).GetProperties().FirstOrDefault((PropertyInfo x) => x.Name.Equals(filedName));
			if (propertyInfo != null)
			{
				Tuple<OperateResult, ComDataAttribute> tuple = this.CheckReadAdr(propertyInfo);
				if (!tuple.Item1.IsSuccess)
				{
					return new Tuple<OperateResult<byte[]>, PropertyInfo>(new OperateResult<byte[]>
					{
						IsSuccess = false,
						Message = tuple.Item1.Message
					}, propertyInfo);
				}
				ComDataAttribute item = tuple.Item2;
				len = ((len == 0) ? this.GetByteCount(propertyInfo, item) : len);
				OperateResult<byte[]> item2 = this._readWriteNet.Read(item.Adr, len);
				return new Tuple<OperateResult<byte[]>, PropertyInfo>(item2, propertyInfo);
			}
			return new Tuple<OperateResult<byte[]>, PropertyInfo>(new OperateResult<byte[]>
			{
				IsSuccess = false,
				Message = "目标属性不存在"
			}, null);
		}

		public Tuple<T, OperateResult> ReadEntityMuti<T>(T dstT, Expression<Func<T, object>> exp, bool isSerialRead = false) where T : class, new()
		{
			Tuple<T, OperateResult> tuple = this.ReadEntityMuti(exp, isSerialRead);
			if (tuple.Item2.IsSuccess)
			{
				NetWorkDeviceOpr.EntityCopy(tuple.Item1, dstT);
			}
			return tuple;
		}

		public Tuple<T, OperateResult> ReadEntityMuti<T>(Expression<Func<T, object>> exp, bool isSerialRead = false) where T : class, new()
		{
			if (!this._iConnectState.IsConnected)
			{
				OperateResult operateResult = this._iConnectState.ConnectServer();
				if (!operateResult.IsSuccess)
				{
					return new Tuple<T, OperateResult>(new T(), operateResult);
				}
			}
			List<string> columnNames = LambdaResolver.GetColumnNames(exp);
			if (columnNames.Count == 0)
			{
				return new Tuple<T, OperateResult>(new T(), new OperateResult
				{
					IsSuccess = false,
					Message = "目标属性不存在！"
				});
			}
			if (columnNames.Count == 1)
			{
				isSerialRead = false;
			}
			Type typeFromHandle = typeof(T);
			List<PropertyInfo> list = typeFromHandle.GetProperties().ToList();
			SortedDictionary<int, PropertyInfo> sortedDictionary = new SortedDictionary<int, PropertyInfo>();
			T val = new T();
			OperateResult item = new OperateResult
			{
				IsSuccess = true,
				Message = "成功"
			};
			if (!this._iConnectState.IsConnected && !isSerialRead)
			{
				this._iConnectState.ConnectServer();
			}
			foreach (string name in columnNames)
			{
				if (!isSerialRead)
				{
					PropertyInfo propertyInfo = list.Find((PropertyInfo x) => x.Name.Equals(name));
					if (!(propertyInfo == null))
					{
						ComDataAttribute comDataAttribute = Attribute.GetCustomAttribute(propertyInfo, typeof(ComDataAttribute)) as ComDataAttribute;
						if (comDataAttribute != null && !comDataAttribute.Adr.Trim().Equals(string.Empty) && comDataAttribute.OprType.HasFlag(OprTypeEnum.Read))
						{
							ushort byteCount = this.GetByteCount(propertyInfo, comDataAttribute);
							ReadQueue readQueue = default(ReadQueue);
							readQueue.DataType = comDataAttribute.DataType;
							readQueue.StartIndex = 0;
							readQueue.BitIndex = comDataAttribute.BitIndex;
							readQueue.Property = propertyInfo;
							ReadQueue value = readQueue;
							this.Read(new Dictionary<string, ReadQueue> { { propertyInfo.Name, value } }, val, comDataAttribute.Adr, byteCount);
						}
					}
				}
				else
				{
					int num = list.FindIndex((PropertyInfo x) => x.Name.Equals(name));
					if (num != -1)
					{
						sortedDictionary.Add(num, list[num]);
					}
				}
			}
			if (!isSerialRead)
			{
				return new Tuple<T, OperateResult>(val, item);
			}
			return this.SetPropertiesValue<T>(sortedDictionary);
		}

		private Tuple<T, OperateResult> SetPropertiesValue<T>(SortedDictionary<int, PropertyInfo> properties) where T : class, new()
		{
			if (!this._iConnectState.IsConnected)
			{
				OperateResult operateResult = this._iConnectState.ConnectServer();
				if (!operateResult.IsSuccess)
				{
					return new Tuple<T, OperateResult>(new T(), operateResult);
				}
			}
			string text = "";
			ushort num = 0;
			Dictionary<string, ReadQueue> dictionary = new Dictionary<string, ReadQueue>();
			List<string> list = new List<string>();
			T val = new T();
			OperateResult item = new OperateResult
			{
				IsSuccess = true,
				Message = "成功"
			};
			foreach (int key in properties.Keys)
			{
				ComDataAttribute comDataAttribute = Attribute.GetCustomAttribute(properties[key], typeof(ComDataAttribute)) as ComDataAttribute;
				if (comDataAttribute == null || (!comDataAttribute.OprType.HasFlag(OprTypeEnum.Read) && list.Count == 0) || comDataAttribute.OprType == OprTypeEnum.None || string.IsNullOrWhiteSpace(comDataAttribute.Adr))
				{
					continue;
				}
				bool flag = properties.Keys.Contains(key + 1);
				if (list.Count <= 0)
				{
					if (!flag)
					{
						comDataAttribute.IsSerial = SerialFlagEnum.No;
					}
					else
					{
						ComDataAttribute comDataAttribute2 = Attribute.GetCustomAttribute(properties[key + 1], typeof(ComDataAttribute)) as ComDataAttribute;
						if (comDataAttribute2 == null)
						{
							comDataAttribute.IsSerial = SerialFlagEnum.No;
						}
						else if (comDataAttribute2.IsSerial == SerialFlagEnum.No)
						{
							comDataAttribute.IsSerial = SerialFlagEnum.No;
						}
					}
				}
				ushort byteCount = this.GetByteCount(properties[key], comDataAttribute);
				ReadQueue readQueue;
				if (comDataAttribute.IsSerial == SerialFlagEnum.Yes || comDataAttribute.IsSerial == SerialFlagEnum.End)
				{
					if (text == "")
					{
						text = comDataAttribute.Adr;
					}
					if (!list.Contains(comDataAttribute.Adr))
					{
						num = (ushort)(num + byteCount);
						list.Add(comDataAttribute.Adr);
					}
					else if (list.Contains(comDataAttribute.Adr) && properties[key].PropertyType != typeof(bool))
					{
						throw new Exception("地址重复！");
					}
					string name = properties[key].Name;
					readQueue = new ReadQueue
					{
						Property = properties[key],
						DataType = comDataAttribute.DataType,
						StartIndex = num - byteCount,
						BitIndex = comDataAttribute.BitIndex
					};
					dictionary.Add(name, readQueue);
					if (!flag)
					{
						OperateResult operateResult2 = this.Read(dictionary, val, text, num);
						if (!operateResult2.IsSuccess)
						{
							continue;
						}
						text = "";
						num = 0;
						list.Clear();
						dictionary.Clear();
					}
					if (properties[key].Name.Equals(properties[properties.Keys.Last()].Name))
					{
						break;
					}
				}
				else
				{
					readQueue = default(ReadQueue);
					readQueue.DataType = comDataAttribute.DataType;
					readQueue.StartIndex = 0;
					readQueue.BitIndex = comDataAttribute.BitIndex;
					readQueue.Property = properties[key];
					ReadQueue value = readQueue;
					OperateResult operateResult3 = this.Read(new Dictionary<string, ReadQueue> { 
					{
						properties[key].Name,
						value
					} }, val, comDataAttribute.Adr, byteCount);
					if (operateResult3.IsSuccess)
					{
					}
				}
			}
			return new Tuple<T, OperateResult>(val, item);
		}

		public override Tuple<T, OperateResult> ReadEntityMuti<T>(T dstT)
		{
			Tuple<T, OperateResult> tuple = this.ReadEntityMuti<T>();
			if (tuple.Item2.IsSuccess)
			{
				NetWorkDeviceOpr.EntityCopy(tuple.Item1, dstT);
			}
			return tuple;
		}

		public Tuple<T, OperateResult> ReadEntityMuti<T>() where T : class, new()
		{
			Type typeFromHandle = typeof(T);
			PropertyInfo[] properties = typeFromHandle.GetProperties();
			return this.SetPropertiesValue<T>(properties);
		}

		private Tuple<T, OperateResult> SetPropertiesValue<T>(PropertyInfo[] properties) where T : class, new()
		{
			if (!this._iConnectState.IsConnected)
			{
				OperateResult operateResult = this._iConnectState.ConnectServer();
				if (!operateResult.IsSuccess)
				{
					return new Tuple<T, OperateResult>(new T(), operateResult);
				}
			}
			string text = "";
			ushort num = 0;
			Dictionary<string, ReadQueue> dictionary = new Dictionary<string, ReadQueue>();
			List<string> list = new List<string>();
			T val = new T();
			OperateResult operateResult2 = new OperateResult
			{
				IsSuccess = true,
				Message = ""
			};
			foreach (PropertyInfo propertyInfo in properties)
			{
				ComDataAttribute comDataAttribute = Attribute.GetCustomAttribute(propertyInfo, typeof(ComDataAttribute)) as ComDataAttribute;
				if (comDataAttribute == null || (!comDataAttribute.OprType.HasFlag(OprTypeEnum.Read) && list.Count == 0) || comDataAttribute.OprType == OprTypeEnum.None || string.IsNullOrWhiteSpace(comDataAttribute.Adr) || comDataAttribute.Ignore)
				{
					continue;
				}
				ushort byteCount = this.GetByteCount(propertyInfo, comDataAttribute);
				ReadQueue readQueue;
				if (comDataAttribute.IsSerial == SerialFlagEnum.Yes || comDataAttribute.IsSerial == SerialFlagEnum.End)
				{
					if (text == "")
					{
						text = comDataAttribute.Adr;
					}
					Type propertyType = base.GetPropertyType(propertyInfo);
					if (!list.Contains(comDataAttribute.Adr))
					{
						num = (ushort)(num + byteCount);
						list.Add(comDataAttribute.Adr);
					}
					else if (list.Contains(comDataAttribute.Adr) && propertyType != typeof(bool))
					{
						throw new Exception("地址重复！");
					}
					string name = propertyInfo.Name;
					readQueue = new ReadQueue
					{
						Property = propertyInfo,
						DataType = comDataAttribute.DataType,
						StartIndex = num - byteCount,
						ByteCount = byteCount,
						BitIndex = comDataAttribute.BitIndex
					};
					dictionary.Add(name, readQueue);
					if (!propertyInfo.Name.Equals(properties.Last().Name) && comDataAttribute.IsSerial != SerialFlagEnum.End)
					{
						continue;
					}
				}
				if (list.Count != 0)
				{
					OperateResult operateResult3 = this.Read(dictionary, val, text, num);
					if (!operateResult3.IsSuccess)
					{
						operateResult2.IsSuccess = false;
						operateResult2.Message += operateResult3.Message;
					}
					text = "";
					num = 0;
					list.Clear();
					dictionary.Clear();
					if (!propertyInfo.Name.Equals(properties.Last().Name))
					{
						continue;
					}
					break;
				}
				readQueue = default(ReadQueue);
				readQueue.DataType = comDataAttribute.DataType;
				readQueue.StartIndex = 0;
				readQueue.ByteCount = byteCount;
				readQueue.BitIndex = comDataAttribute.BitIndex;
				readQueue.Property = propertyInfo;
				ReadQueue value = readQueue;
				OperateResult operateResult4 = this.Read(new Dictionary<string, ReadQueue> { { propertyInfo.Name, value } }, val, comDataAttribute.Adr, byteCount);
				if (!operateResult4.IsSuccess)
				{
					operateResult2.IsSuccess = false;
					OperateResult operateResult5 = operateResult2;
					operateResult5.Message = operateResult5.Message + "地址" + comDataAttribute.Adr + operateResult4.Message + Environment.NewLine;
				}
			}
			return new Tuple<T, OperateResult>(val, operateResult2);
		}

		private OperateResult Read<T>(Dictionary<string, ReadQueue> dic, T t, string startAdr, ushort readLen)
		{
			OperateResult operateResult = new OperateResult
			{
				Message = ""
			};
			try
			{
				SiemensS7Net siemensS7Net = this._readWriteNet as SiemensS7Net;
				if (siemensS7Net == null)
				{
					MelsecMcNet melsecMcNet = this._readWriteNet as MelsecMcNet;
					if (melsecMcNet != null)
					{
						if (dic.Values.Any((ReadQueue x) => x.Property.PropertyType == typeof(bool)))
						{
							OperateResult<bool[]> operateResult2 = melsecMcNet.ReadBool(startAdr, readLen);
							for (int i = 0; i < dic.Keys.Count; i++)
							{
								KeyValuePair<string, ReadQueue> keyValuePair = dic.ElementAt(i);
								if (i < readLen && operateResult2?.Content != null)
								{
									DataConverter.PropertySetValue(t, keyValuePair.Value.Property, operateResult2.Content[i]);
									base.CommRecord<ComDataAttribute>(keyValuePair.Value.Property, operateResult2.Content[i], OprTypeEnum.Read);
								}
							}
							return operateResult2;
						}
						readLen = (ushort)((int)readLen / 2);
					}
				}
				OperateResult<byte[]> operateResult3 = this._readWriteNet.Read(startAdr, readLen);
				if (!operateResult3.IsSuccess)
				{
					operateResult.IsSuccess = false;
					string message = operateResult.Message;
					DefaultInterpolatedStringHandler defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(4, 3);
					defaultInterpolatedStringHandler.AppendLiteral("地址");
					defaultInterpolatedStringHandler.AppendFormatted(startAdr);
					defaultInterpolatedStringHandler.AppendFormatted(operateResult.Message);
					defaultInterpolatedStringHandler.AppendLiteral("异常");
					defaultInterpolatedStringHandler.AppendFormatted(Environment.NewLine);
					operateResult.Message = message + defaultInterpolatedStringHandler.ToStringAndClear();
					return operateResult;
				}
				foreach (string key in dic.Keys)
				{
					dynamic valueDynamic = base.GetValueDynamic(dic[key], operateResult3.Content);
					DataConverter.PropertySetValue(t, dic[key].Property, valueDynamic);
					CommRecord<ComDataAttribute>(dic[key].Property, valueDynamic, OprTypeEnum.Read);
				}
				return new OperateResult
				{
					IsSuccess = true
				};
			}
			catch (Exception ex)
			{
				return new OperateResult
				{
					Message = ex.ToString()
				};
			}
		}

		public override OperateResult WriteEntitySingle(Type entityType, string filedName, dynamic value, ushort attempTimes = 0, ushort attempInterval = 30)
		{
			if ((object)entityType == null)
			{
				throw new ArgumentNullException("entityType");
			}
			if (string.IsNullOrEmpty(filedName))
			{
				return new OperateResult
				{
					IsSuccess = false,
					Message = "字段名称不能为空"
				};
			}
			PropertyInfo property = entityType.GetProperty(filedName);
			ComDataAttribute comDataAttribute = Attribute.GetCustomAttribute(property ?? throw new InvalidOperationException(), typeof(ComDataAttribute)) as ComDataAttribute;
			if (comDataAttribute == null)
			{
				return new OperateResult
				{
					IsSuccess = false,
					Message = "当前属性未标记ComDataAttribute"
				};
			}
			if (string.IsNullOrWhiteSpace(comDataAttribute.Adr))
			{
				return new OperateResult
				{
					IsSuccess = false,
					Message = "目标属性" + property.Name + "地址为空！"
				};
			}
			try
			{
				this.GetValue(property, comDataAttribute, ref value);
			}
			catch (RuntimeBinderException ex)
			{
				throw ex;
			}
			OperateResult operateResult = Write(property, comDataAttribute, value);
			if (!operateResult.IsSuccess && attempTimes > 0)
			{
				if (attempInterval > 0)
				{
					Thread.Sleep(attempInterval);
				}
				attempTimes = (ushort)(attempTimes - 1);
				WriteEntitySingle(entityType, filedName, value, attempTimes);
			}
			return operateResult;
		}

		public override OperateResult WriteEntitySingle<T>(string filedName, dynamic value, ushort attempTimes = 0, ushort attempInterval = 30)
		{
			return WriteEntitySingle(typeof(T), filedName, value, attempTimes, attempInterval);
		}

		public override OperateResult WriteEntitySingle<T>(Expression<Func<T, object>> exp, dynamic value, ushort attempTimes = 0, ushort attempInterval = 30)
		{
			List<string> columnNames = LambdaResolver.GetColumnNames(exp);
			if (columnNames.Count == 0)
			{
				throw new Exception("目标地址不存在！");
			}
			if (columnNames.Count > 1)
			{
				throw new Exception("目标地址只能一个！");
			}
			return WriteEntitySingle<T>(columnNames.First(), value, attempTimes);
		}

		private OperateResult Write(PropertyInfo property, ComDataAttribute comAb, dynamic value)
		{
			this.GetValue(property.PropertyType, comAb, ref value);
			OperateResult operateResult = new OperateResult
			{
				IsSuccess = false,
				Message = "未实现PLC"
			};
			SiemensS7Net siemensS7Net = this._readWriteNet as SiemensS7Net;
			if (siemensS7Net != null)
			{
				Type propertyType = base.GetPropertyType(property.PropertyType);
				if (propertyType == typeof(bool))
				{
					DefaultInterpolatedStringHandler defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(1, 2);
					defaultInterpolatedStringHandler.AppendFormatted(comAb.Adr);
					defaultInterpolatedStringHandler.AppendLiteral(".");
					defaultInterpolatedStringHandler.AppendFormatted(comAb.BitIndex);
					operateResult = siemensS7Net.Write(defaultInterpolatedStringHandler.ToStringAndClear(), value);
				}
				else
				{
					byte[] value2 = EnDeCode.ConvertToByteBuf(value, base.EndianMode, strEncode: comAb.StringEncode);
					operateResult = this._readWriteNet.Write(comAb.Adr, value2);
				}
			}
			else
			{
				MelsecMcNet melsecMcNet = this._readWriteNet as MelsecMcNet;
				if (melsecMcNet != null)
				{
					operateResult = melsecMcNet.Write(comAb.Adr, value);
				}
			}
			if (operateResult.IsSuccess)
			{
				CommRecord<ComDataAttribute>(property, value, OprTypeEnum.Write);
			}
			return operateResult;
		}

		public override OperateResult WriteEntityMuti<T>(T entity, Expression<Func<T, object>> exp, ushort attempTimes = 0, ushort attempInterval = 30)
		{
			List<string> columnNames = LambdaResolver.GetColumnNames(exp);
			if (columnNames.Count == 0)
			{
				throw new Exception("目标地址不存在！");
			}
			OperateResult operateResult = new OperateResult
			{
				IsSuccess = true,
				Message = "成功"
			};
			Type typeFromHandle = typeof(T);
			foreach (string item in columnNames)
			{
				PropertyInfo property = typeFromHandle.GetProperty(item);
				ComDataAttribute comDataAttribute = Attribute.GetCustomAttribute(property ?? throw new InvalidOperationException(), typeof(ComDataAttribute)) as ComDataAttribute;
				if (comDataAttribute == null)
				{
					return new OperateResult
					{
						IsSuccess = false,
						Message = "当前属性未标记ComDataAttribute"
					};
				}
				dynamic value = property.GetValue(entity);
				this.GetValue(property.PropertyType, comDataAttribute, ref value);
				OperateResult operateResult2 = Write(property, comDataAttribute, value);
				if (!operateResult2.IsSuccess && attempTimes > 0)
				{
					for (int i = 0; i < attempTimes; i++)
					{
						if (attempInterval > 0)
						{
							Thread.Sleep(attempInterval);
						}
						operateResult2 = Write(property, comDataAttribute, value);
						if (operateResult2.IsSuccess)
						{
							break;
						}
					}
				}
				if (!operateResult2.IsSuccess)
				{
					operateResult.IsSuccess = false;
					operateResult.Message = operateResult.Message + operateResult2.Message + item;
				}
			}
			return operateResult;
		}

		public static void EntityCopy<T>(T srcEntity, T dstEntity)
		{
			IEnumerable<PropertyInfo> enumerable = typeof(T).GetProperties().Where(delegate(PropertyInfo x)
			{
				ComDataAttribute comDataAttribute = Attribute.GetCustomAttribute(x, typeof(ComDataAttribute)) as ComDataAttribute;
				return comDataAttribute != null && comDataAttribute.OprType.HasFlag(OprTypeEnum.Read);
			});
			foreach (PropertyInfo item in enumerable)
			{
				object value = item.GetValue(srcEntity);
				item.SetValue(dstEntity, item.GetValue(srcEntity));
			}
		}

		public static List<string> GetDstOprTypeInfo<T>(OprTypeEnum oprType = OprTypeEnum.Write) where T : class, new()
		{
			return (from x in typeof(T).GetProperties().Where(delegate(PropertyInfo x)
				{
					ComDataAttribute comDataAttribute = Attribute.GetCustomAttribute(x, typeof(ComDataAttribute)) as ComDataAttribute;
					return comDataAttribute != null && comDataAttribute.OprType.HasFlag(oprType);
				})
				select x.Name).ToList();
		}

		public static List<string> GetDstOprTypeInfo(Type entityType, OprTypeEnum oprType = OprTypeEnum.Write)
		{
			return (from x in entityType.GetProperties().Where(delegate(PropertyInfo x)
				{
					ComDataAttribute comDataAttribute = Attribute.GetCustomAttribute(x, typeof(ComDataAttribute)) as ComDataAttribute;
					return comDataAttribute != null && comDataAttribute.OprType.HasFlag(oprType);
				})
				select x.Name).ToList();
		}

		public static string GetDescribe<T>(string propertyName)
		{
			if (string.IsNullOrWhiteSpace(propertyName))
			{
				return "";
			}
			PropertyInfo propertyInfo = typeof(T).GetProperties().FirstOrDefault((PropertyInfo x) => x.Name.Equals(propertyName));
			if (propertyInfo != null)
			{
				ComDataAttribute comDataAttribute = Attribute.GetCustomAttribute(propertyInfo, typeof(ComDataAttribute)) as ComDataAttribute;
				if (comDataAttribute != null)
				{
					DefaultInterpolatedStringHandler defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(20, 6);
					defaultInterpolatedStringHandler.AppendLiteral(" 地址:");
					defaultInterpolatedStringHandler.AppendFormatted(comDataAttribute.Adr);
					defaultInterpolatedStringHandler.AppendLiteral("位:");
					defaultInterpolatedStringHandler.AppendFormatted(comDataAttribute.BitIndex);
					defaultInterpolatedStringHandler.AppendLiteral(" Name:");
					defaultInterpolatedStringHandler.AppendFormatted(propertyInfo.Name);
					defaultInterpolatedStringHandler.AppendLiteral(" 描述:");
					defaultInterpolatedStringHandler.AppendFormatted(comDataAttribute.Describe);
					defaultInterpolatedStringHandler.AppendFormatted(Environment.NewLine);
					defaultInterpolatedStringHandler.AppendLiteral(" 类型:");
					defaultInterpolatedStringHandler.AppendFormatted(propertyInfo.PropertyType);
					return defaultInterpolatedStringHandler.ToStringAndClear();
				}
			}
			return "";
		}

		public static string ReadValueFormat<T>(T entity) where T : class, new()
		{
			if (entity == null)
			{
				throw new ArgumentNullException("entity");
			}
			string text = "";
			PropertyInfo[] properties = entity.GetType().GetProperties();
			PropertyInfo[] array = properties;
			foreach (PropertyInfo propertyInfo in array)
			{
				ComDataAttribute comDataAttribute = Attribute.GetCustomAttribute(propertyInfo, typeof(ComDataAttribute)) as ComDataAttribute;
				if (comDataAttribute == null || !comDataAttribute.OprType.HasFlag(OprTypeEnum.Read))
				{
					continue;
				}
				object obj = propertyInfo.GetValue(entity);
				DefaultInterpolatedStringHandler defaultInterpolatedStringHandler;
				if (propertyInfo.PropertyType == typeof(Array))
				{
					Array array2 = (Array)obj;
					obj = "";
					if (array2 != null)
					{
						obj = "";
						foreach (object item in array2)
						{
							string? obj2 = obj?.ToString();
							defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(3, 1);
							defaultInterpolatedStringHandler.AppendLiteral("0X");
							defaultInterpolatedStringHandler.AppendFormatted(item, "X2");
							defaultInterpolatedStringHandler.AppendLiteral(" ");
							obj = obj2 + defaultInterpolatedStringHandler.ToStringAndClear();
						}
					}
				}
				string text2 = text;
				defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(27, 7);
				defaultInterpolatedStringHandler.AppendLiteral(" 地址:");
				defaultInterpolatedStringHandler.AppendFormatted(comDataAttribute.Adr);
				defaultInterpolatedStringHandler.AppendLiteral("位:");
				defaultInterpolatedStringHandler.AppendFormatted(comDataAttribute.BitIndex);
				defaultInterpolatedStringHandler.AppendLiteral(" Value:");
				defaultInterpolatedStringHandler.AppendFormatted(obj?.ToString().Trim());
				defaultInterpolatedStringHandler.AppendLiteral(" Name:");
				defaultInterpolatedStringHandler.AppendFormatted(propertyInfo.Name);
				defaultInterpolatedStringHandler.AppendLiteral(" 描述:");
				defaultInterpolatedStringHandler.AppendFormatted(comDataAttribute.Describe);
				defaultInterpolatedStringHandler.AppendLiteral(" 类型:");
				defaultInterpolatedStringHandler.AppendFormatted(propertyInfo.PropertyType);
				defaultInterpolatedStringHandler.AppendFormatted(Environment.NewLine);
				text = text2 + defaultInterpolatedStringHandler.ToStringAndClear();
			}
			return text;
		}

		public override DataTable CreateDsSchema<T>()
		{
			return base.CreateDsSchema<T, ComDataAttribute>();
		}
	}
}
